/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.audit.controller  
 */
package com.apache.audit.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.action.ActionSupport;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.audit.entity.LogModel;
import com.apache.audit.entity.LogModelDetail;
import com.apache.audit.manager.LogModelDetailManager;
import com.apache.audit.manager.LogModelManager;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;

/**
 * description:  日志模型配置管理Action
 * @author dwusoft 创建时间：2015-4-9  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
@Controller
@RequestMapping("/audit/model/")
public class LogModelAction extends ActionSupport<LogModel> {

	@Autowired
	private LogModelManager logModelManager;
	@Autowired
	private LogModelDetailManager logModelDetailManager;

	/**
	 * TODO ajax日志模型配置列表.  
	 */
	@RequestMapping("model!data.action")
	@ResponseBody
	protected Object data(HttpServletRequest request, String page, String rows) {
		ParamsVo<LogModel> vo = new ParamsVo<LogModel>();
		LogModel model = new LogModel();
		if (Validator.isNotNull(request.getParameter("modelEname"))) {
			model.setModelEname(request.getParameter("modelEname"));
		}
		vo.setParams("pageIndex", page);
		vo.setParams("pageSize", rows);
		vo.setObj(model);
		Page pageList = logModelManager.getPageInfo(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", pageList.getCount());
		mapData.put("rows", pageList.getPageObjects());
		return mapData;
	}

	/**
	 * 功能说明：跳转到新增页面
	 */
	@Token(save = true)
	@RequestMapping(value = "add.action", method = RequestMethod.GET)
	protected ModelAndView add(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(ADD);
		LogModel model = new LogModel();
		if (Validator.isNotNull(request.getParameter("id"))) {
			ParamsVo<LogModel> vo = new ParamsVo<LogModel>();
			vo.setInfoId(request.getParameter("id"));
			model = (LogModel) logModelManager.getInfoById(vo);
		}
		view.addObject("item", model);
		return view;
	}

	/**
	 * 功能说明：通用保存页面
	 */
	@Token(remove = true)
	@RequestMapping(value = "/save.action", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg save(HttpServletRequest request, HttpServletResponse response, @ModelAttribute LogModel item) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		ParamsVo<LogModel> vo = new ParamsVo<LogModel>();
		if (Validator.isNotNull(item.getModelId())) {//对信息修改
			vo.setObj(item);
			boolean mark = getBaseManager().editInfo(vo);
			if (!mark) {
				rmsg = getResultMsg(request, "信息修改失败！");
			}
		} else {//添加信息
			vo.setObj(item);
			String id = getBaseManager().saveInfo(vo);
			if (Validator.isNull(id)) {
				rmsg = getResultMsg(request, "信息添加失败！");
			}
		}
		return rmsg;
	}

	/**
	 * 功能说明：删除功能
	 * @param response HttpServletResponse
	 * @param id   删除的id
	 */
	@RequestMapping(value = "/del.action")
	@ResponseBody
	public ResultMsg delete(HttpServletRequest request, String id) {
		ResultMsg rmsg = new ResultMsg("T", "删除成功！");
		ParamsVo<LogModel> vo = new ParamsVo<LogModel>();
		if (isNotNull(id)) {
			vo.setInfoId(id);
			ParamsVo<LogModelDetail> lmd = new ParamsVo<LogModelDetail>();
			LogModelDetail lmdl = new LogModelDetail();
			lmdl.setModelId(id);
			long count = logModelDetailManager.countInfo(lmd);
			if (count > 0) {
				rmsg = getResultMsg(request, "此模型下存在相关属性值,不能删除!");
			} else {
				boolean mark = getBaseManager().deleteInfo(vo);
				if (!mark) {
					rmsg.setFlag("F");
					rmsg.setMsg("删除失败！");
				}
			}
		} else {
			rmsg.setFlag("H");
			rmsg.setMsg("请不要恶意操作！");
		}
		return rmsg;
	}

	protected BaseManager<LogModel> getBaseManager() {
		return logModelManager;
	}

	protected String getPrefix() {
		return "audit/log-model-";
	}

}
